package com.etcn.modules.busi.member.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.etcn.common.exception.BusinessException;
import com.etcn.common.service.impl.CrudServiceImpl;
import com.etcn.common.utils.StringUtil;
import com.etcn.common.utils.UserUtil;
import com.etcn.modules.busi.member.dao.IWmMemberAttentionDao;
import com.etcn.modules.busi.member.pojo.WmMemberAttention;
import com.etcn.modules.busi.member.service.IWmMemberAttentionService;

import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 会员关注serviceImpl
 * 
 * @author guozc
 * @date 2018-03-22 09:08:10
 */
@Service("wmMemberAttentionService")
public class WmMemberAttentionServiceImpl extends CrudServiceImpl<IWmMemberAttentionDao, WmMemberAttention>
		implements IWmMemberAttentionService {

	@SuppressWarnings("unchecked")
	@Override
	public void add(Map<String, Object> param) throws Exception {
		// 参数校验
		if(param == null){
			throw new BusinessException("参数不能为空");
		}
		String type = (String)param.get("type");
		String typeCode = (String)param.get("typeCode");
		Map<String,Object> showinfo = (Map<String,Object>)param.get("showinfo");
		if(StringUtil.isEmpty(type)){
			throw new BusinessException(type + "参数不能为空");
		}
		if(StringUtil.isEmpty(typeCode)){
			throw new BusinessException(typeCode + "参数不能为空");
		}
		if(showinfo == null || showinfo.isEmpty()){
			throw new BusinessException(showinfo + "参数不能为空");
		}
		// 判断是否关注过
		WmMemberAttention entity = new WmMemberAttention();
		entity.setType(type);
		entity.setTypeCode(typeCode);
		entity.setMemberId(UserUtil.getCurrentMemberId());
		if(judgeIfAttention(entity)){
			throw new BusinessException("已经关注过了");
		}
		
		WmMemberAttention attention = new WmMemberAttention();
		attention.setMemberId(UserUtil.getCurrentMemberId());
		attention.setType(type);
		attention.setTypeCode(typeCode);
		for(Map.Entry<String, Object> entry : showinfo.entrySet()){
			attention.setShowColName(entry.getKey());
			System.out.println(entry.getValue().getClass().getName());
			Object colValue = entry.getValue();
			if(colValue != null){
				if(colValue instanceof String){
					attention.setShowColValue((String)entry.getValue());
					attention.setShowColType("1");
				}else if(colValue instanceof JSONArray){
					attention.setShowColValue(JSONArray.toJSONString(colValue));
					attention.setShowColType("2");
				}else if(colValue instanceof JSONObject){
					attention.setShowColValue(JSONObject.toJSONString(colValue));
					attention.setShowColType("3");
				}
			}else{
				attention.setShowColValue("");
				attention.setShowColType("1");
			}
			insert(attention);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<Map<String,Object>> findByMemberId(WmMemberAttention entity) throws Exception {
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("memberId", UserUtil.getCurrentMemberId());
		String[] types = null;
		String group = entity.getGroup();
		if("1".equals(group)){
			types = new String[]{"1","2","3","4"};
		}else if("2".equals(group)){
			types = new String[]{"5"};
		}
		param.put("types", types);
		
		List<WmMemberAttention> attentions = dao.findByMemberId(param);
		Map<String,Map<String,Object>> index = new HashMap<String,Map<String,Object>>();
		for(WmMemberAttention attention : attentions){
			Map<String,Object> codeAttention = index.get(attention.getType()+"-"+attention.getTypeCode());
			if(codeAttention == null){
				codeAttention = new HashMap<String,Object>();
				Map<String,Object> showinfo = new HashMap<String,Object>();
				codeAttention.put("showinfo", showinfo);
				index.put(attention.getType()+"-"+attention.getTypeCode(), codeAttention);
			}
			codeAttention.put("type", attention.getType());
			codeAttention.put("typeCode", attention.getTypeCode());
			Map<String,Object> showinfo = (Map<String,Object>)codeAttention.get("showinfo");

			String colValue = attention.getShowColValue();
			String colType = attention.getShowColType();
			if(StringUtil.isNotEmpty(colValue)){
				if("1".equals(colType)){
					showinfo.put(attention.getShowColName(), colValue);
				}else if("2".equals(colType)){
					showinfo.put(attention.getShowColName(), JSON.parseArray(colValue));
				}else if("3".equals(colType)){
					showinfo.put(attention.getShowColName(), JSON.parseObject(colValue));
				}
			}else{
				showinfo.put(attention.getShowColName(), "");
			}

		}
		return index.values();
	}

	@Override
	public void cancelAttentions(List<Map<String, String>> attentions) throws Exception {
		if(attentions != null){
			String memberId = UserUtil.getCurrentMemberId();
			for(Map<String, String> attention : attentions){
				attention.put("memberId", memberId);
				dao.deleteMembersAttention(attention);
			}
		}
	}

	@Override
	public boolean judgeIfAttention(WmMemberAttention attention) throws Exception {
		int count = dao.findCountByMember(attention);
		return count > 0;
	}

}